#include once "mysql\mysql.bi"
#include once "md5.bi"
#define NULL 0

dim conn as MYSQL ptr
conn = mysql_init( NULL )
mysql_real_connect(conn, "localhost", "root", "root", NULL, MYSQL_PORT, NULL, 0)


Sub createdb(ByRef conn as MYSQL ptr)
	mysql_query(conn, "CREATE DATABASE tmp123")
End Sub

Sub createtable(ByRef conn as MYSQL ptr)
	mysql_query(conn, "USE tmp123")
	mysql_query(conn, "CREATE TABLE usertest (`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,`username` CHAR(32) NOT NULL,`passwd` CHAR(32) NOT NULL,`email` CHAR(100) NOT NULL,`hash` CHAR(32) NOT NULL,PRIMARY KEY (`id`)  )")
End Sub

Sub insertrows(ByRef conn as MYSQL ptr)
	dim randn as string
	dim hash as string
	dim i as integer
	For i = 1 To 90001
		Randomize
		randn = str(Rnd * (999-111) + 111)
		hash = MD5_FromString("example"+str(i)+randn+"example"+str(i)+"@example.com")
		mysql_query(conn, "INSERT INTO usertest (`id`,`username`,`passwd`,`email`,`hash`) VALUES ( NULL,'example"+str(i)+"','"+randn+"','example"+str(i)+"@example.com','"+hash+"')")
	Next i
End Sub

Sub selectrows(ByRef conn as MYSQL ptr)
	dim res as MYSQL_RES ptr
	dim row as MYSQL_ROW
	dim newhash as string
	mysql_query(conn, "SELECT * FROM usertest")
	res = mysql_use_result(conn)
	while row = mysql_fetch_row(res)
		newhash = MD5_FromString(str(row[1])+str(row[2])+str(row[3]))
		if newhash <> str(row[4]) then
			print "Something went wrong!"
		end if
	wend
End Sub

Sub deleterows(ByRef conn as MYSQL ptr)
	mysql_query(conn, "DELETE FROM usertest")
End Sub

Sub deletetable(ByRef conn as MYSQL ptr)
	mysql_query(conn, "DROP TABLE usertest")
End Sub

Sub deletedb(ByRef conn as MYSQL ptr)
	mysql_query(conn, "DROP DATABASE tmp123")
	mysql_close(conn)
End Sub

createdb(conn)
createtable(conn)
insertrows(conn)
selectrows(conn)
deleterows(conn)
deletetable(conn)
deletedb(conn)
